package cn.acewill.yxd.yunxundian.ui.user;

import android.util.Log;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.Shop;
import cn.acewill.yxd.yunxundian.model.SignUpInfo;

/**
 * Created by XXX on 2017/4/2.
 */
public class DataProvider {

    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());

    public  List<Schedule> getScheduleTask(int employeeid){
        List<Schedule> list=null;
        try {
            //db.selector(Schedule.class).select("")
            list= db.selector(Schedule.class).where("toUserId", "=", String.valueOf(employeeid)).and("states",">=","1").and("states","<=","2").and("title","!=","年假").and("title","!=","休息").and("title","!=","其他假").and("shopId",">","0").findAll();
            SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
            Date now = new Date();
            if(list!=null) {
                for (int i = 0; i < list.size(); i++) {
                    Schedule ss = list.get(i);
                    String tempTime = ss.getSchStartTime().replace('T', ' ');
                    Date thisDate = sdf.parse(tempTime);
                    if (thisDate.after(now)) {
                        list.remove(i--);
                    }
                }
                ListSort(list);
            }
            //list=db.selector(Schedule.class).select("select ")
        } catch (DbException e) {
            e.printStackTrace();
            list=null;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }


    //获取今日要完成的任务数量
    public List<Schedule> getTodayTask(int employeeid) {
        List<Schedule> list = null;
        try {
            SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd");
            String date =sdf.format(new Date());
            list= db.selector(Schedule.class).where("toUserId", "=", String.valueOf(employeeid)).and("states",">=","1")
                    .and("states","<=","2")
                    .and("title","!=","年假")
                    .and("title","!=","休息")
                    .and("title","!=","其他假")
                    .and("schStartTime","like",date+"%")
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;
    }



    public SignUpInfo getSignupInfo(int employeeid){
        SignUpInfo sui=null;
        try{
            sui=db.selector(SignUpInfo.class).where("empId","=",employeeid+"").and("outTime","=",null).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
            sui=null;
        }
        return sui;
    }

    public Shop getShopinfo(int shopid)
    {
        Shop shop=null;
        try{
            shop=db.selector(Shop.class).where("ID","=",shopid+"").findFirst();
        } catch (DbException e) {
            e.printStackTrace();
            shop=null;
        }
        return shop;
    }

    /**
     * 获取未完成的任务，返回最近的五个日程
     * @param employeeid
     * @return
     */
    public List<Schedule> getUndoScheduleSortByTime(int employeeid){
        List<Schedule> list = null;
        try {
            list= db.selector(Schedule.class).where("toUserId", "=", String.valueOf(employeeid)).and("states",">=","1")
                    .and("states","<=","2")
                    .and("title","!=","年假")
                    .and("title","!=","休息")
                    .and("title","!=","其他假")
                    .findAll();
            //取出小于当前时间的未完成任务
            SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
            Date now = new Date();
            Log.e("time",now.toString());
            if(list!=null) {
                for (int i = 0; i < list.size(); i++) {
                    Schedule ss = list.get(i);
                    String tempTime = ss.getSchStartTime().replace('T', ' ');
                    Date thisDate = null;
                    try {
                        thisDate = sdf.parse(tempTime);
                        if (thisDate.after(now)) {
                            list.remove(i--);
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                ListSort(list);
                Log.e("chaxun",list.size()+"");
                if (list.size()>5){
                    //取前五
                    list = list.subList(list.size()-5,list.size());
                    Log.e("chaxinsub",list.size()+"");
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return  list;
    }
    /*日程排序，从远到近*/
    private  void ListSort(List<Schedule> list) {
        Collections.sort(list, new Comparator<Schedule>() {
            @Override
            public int compare(Schedule o1, Schedule o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date dt1 = format.parse(o1.getSchStartTime());
                    Date dt2 = format.parse(o2.getSchStartTime());
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
}
